package com.superpai.activity;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.superpai.R;
import com.superpai.api.ShopAPI;
import com.superpai.common.Constants;
import com.superpai.domain.Content;
import com.superpai.response.shop.GetSellerMarketActiveResponse;
import com.superpai.view.adapter.ConvenienceAdapter;

/**
 * @author Devin.Hu
 * @date 2011-8-29
 * @version V1.0
 * @description 满减满送活动页面
 */
public class ConvenienceActivity extends BaseActivity {

	private GetSellerMarketActiveResponse response;
	private List<Content> contentList;
	private ConvenienceAdapter adapter;
	private ListView convenience_listview;
	private final int GETCONVENIENCE = 1;
	
	
	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.setContentView(R.layout.convenience);
		initMenuView(this, mIndexBrand);
		initViews();
		
		connectionDate();
	}

	private void initViews(){
		title = (TextView)this.findViewById(R.id.title);
		title.setText(R.string.home_convenience_str);
		
		mProgress = (ProgressBar)this.findViewById(R.id.progress);
		mRefresh = (Button)this.findViewById(R.id.refresh);
		mRefresh.setOnClickListener(this);
		mNodata = (TextView)this.findViewById(R.id.nodata);
		
		adapter = new ConvenienceAdapter(mContext);
		convenience_listview = (ListView)this.findViewById(R.id.convenience_listview);
		convenience_listview.setAdapter(adapter);
	}
	
	/** 读取数据 **/
    private void connectionDate() {
		mRefresh.setVisibility(View.GONE);
		mNodata.setVisibility(View.GONE);
		mProgress.setVisibility(View.VISIBLE);
		onConnection(GETCONVENIENCE);
	}
	
	@Override
	public void onClick(View v) {
		checkMenu(v);
		
		if(v == mRefresh){
			connectionDate();
		}
	}

	@Override
	public Object doDataConnection(int type) {
		try {
			ShopAPI api = new ShopAPI();
			response = api.getSellerMarketActive(Constants.sellerUin);
			if(response.isSuccess()){
				return STATUS_SUC;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return STATUS_ERR;
		}
		return STATUS_NODATA;
	}

	@Override
	public void doProcessData(int type, Object... values) {
		int status = Integer.parseInt(values[0].toString());
		mProgress.setVisibility(View.GONE);
		if(status == STATUS_SUC){
			contentList = response.getContentList();
			adapter.setContentList(contentList);
			adapter.notifyDataSetChanged();
		}else if(status == STATUS_NODATA){
			mNodata.setVisibility(View.VISIBLE);
		}else{
			mRefresh.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		disConnection();
	}
}
